
ESTDesigner.getFieldValue = function (object, field, defualt) {
    defualt != null ? defualt : null;
    if ($.isEmptyObject(object)) {
        return defualt;
    }
    return object[field] != null ? object[field] : defualt;
};

/**
 * 定制图形
 * @param {{width?: number, type: string, title?: string, iconPath?: string}} config 图形配置对象
 */
ESTDesigner.customShape = function (config) {
    var uuid = new Date().getTime();
    var type = ESTDesigner.getFieldValue(config, 'type', "Custom" + uuid);
    ESTDesigner.task[config.type] = ESTDesigner.task.BaseTask.extend({
        init: function (attr, setter, getter) {
            this._super($.extend({
                width: ESTDesigner.getFieldValue(config, 'width', 150),
                type: "ESTDesigner.task." + type,
                title: ESTDesigner.getFieldValue(config, 'title', ""),
                iconPath: ESTDesigner.getFieldValue(config, 'iconPath', 'js/ESTDesigner/icons/type.receive.png')
            }, attr), setter, getter);
            this.setDimension(160, 60);
            this.callElement = null;
            this.inputParams = new draw2d.util.ArrayList();
            this.outputParams = new draw2d.util.ArrayList();
        },
        getStartElementXML: function () {
            var xml = '<' + type + ' ';
            xml = xml + this.getGeneralXML();
            if (this.callElement != null && this.callElement != '')
                xml = xml + ' calledElement="' + this.callElement + '"';
            xml = xml + '>\n';
            return xml;
        },
        getExtensionElementsXML: function () {
            if (this.listeners.getSize() == 0 && this.inputParams.getSize() == 0
                && this.outputParams.getSize() == 0)
                return '';
            var xml = '<extensionElements>\n';
            xml = xml + this.getListenersXML();
            xml = xml + this.getInputParamsXML();
            xml = xml + this.getOutputParamsXML();
            xml = xml + '</extensionElements>\n';
            return xml;
        },
        getInputParamsXML: function () {
            var xml = '';
            for (var i = 0;i < this.inputParams.getSize();i++) {
                var param = this.inputParams.get(i);
                xml = xml + param.toXML();
            }
            return xml;
        },
        getOutputParamsXML: function () {
            var xml = '';
            for (var i = 0;i < this.outputParams.getSize();i++) {
                var param = this.outputParams.get(i);
                xml = xml + param.toXML();
            }
            return xml;
        },
        getEndElementXML: function () {
            var xml = '</' + type + '>\n';
            return xml;
        },
        toXML: function () {
            var xml = this.getStartElementXML();
            xml = xml + this.getExtensionElementsXML();
            xml = xml + this.getMultiInstanceXML();
            xml = xml + this.getEndElementXML();
            return xml;
        },
        getInputParam: function (id) {
            for (var i = 0;i < this.inputParams.getSize();i++) {
                var param = this.inputParams.get(i);
                if (param.id == id) {
                    return param;
                }
            }
        },
        deleteInputParam: function (id) {
            var param = this.getInputParam(id);
            this.inputParams.remove(param);
        },
        getOutputParam: function (id) {
            for (var i = 0;i < this.outputParams.getSize();i++) {
                var param = this.outputParams.get(i);
                if (param.id == id) {
                    return param;
                }
            }
        },
        deleteOutputParam: function (id) {
            var param = this.getOutputParam(id);
            this.outputParams.remove(param);
        }

    });
};

ESTDesigner.customShape({
    type: 'CustomTask',
    title: '定制'
})